package com.keon.jianbao.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.SpannableString;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.keon.jianbao.R;
import com.keon.jianbao.util.StringUtils;
import com.readystatesoftware.viewbadger.BadgeView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by keon on 2015/7/27.
 */
public class ItemLayout extends LinearLayout {

    @Bind(R.id.item)
    LinearLayout llItem;
    @Bind(R.id.icon_frame)
    LinearLayout llicon;
    @Bind(R.id.item_text)
    TextView mText;
    @Bind(R.id.item_icon)
    ImageView mIcon;
    @Bind(R.id.item_title)
    TextView mTitle;
    @Bind(R.id.item_summary)
    TextView mSumarry;
    @Bind(R.id.item_arrow)
    ImageView mArrow;
    @Bind(R.id.item_badge)
    BadgeView bvBadge;


    public ItemLayout(Context context) {
        super(context);
    }

    public ItemLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_item, this);
        ButterKnife.bind(this);

        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.Item, 0, 0);

        String text = typedArray.getString(R.styleable.Item_itemText);
        String title = typedArray.getString(R.styleable.Item_itemTitle);
        String summary = typedArray.getString(R.styleable.Item_itemSummary);
        int icon = typedArray.getResourceId(R.styleable.Item_itemIcon, 0);
        int titleColor = typedArray.getColor(R.styleable.Item_itemTitleColor, 0);
        boolean showBadge = typedArray.getBoolean(R.styleable.Item_showBadge, false);
        boolean showRightArrow = typedArray.getBoolean(R.styleable.Item_showRightArrow, false);

        boolean clickable = typedArray.getBoolean(R.styleable.Item_clickable, true);

        if (!clickable) {
            llItem.setBackgroundResource(R.color.layout_bg_normal);
        }

        mTitle.setText(title);
        if (titleColor != 0) {
            mTitle.setTextColor(titleColor);
        }
        if (icon != 0) {
            llicon.setVisibility(VISIBLE);
            mIcon.setImageResource(icon);

            if (showBadge == true) {
                bvBadge.setVisibility(VISIBLE);
            } else {
                bvBadge.setVisibility(GONE);
            }
        } else {
            llicon.setVisibility(GONE);
        }
        if (StringUtils.isEmpty(text)) {
            mText.setVisibility(GONE);
        } else {
            mText.setText(text);
        }
        if (StringUtils.isEmpty(summary)) {
            mSumarry.setVisibility(GONE);
        } else {
            mSumarry.setText(summary);
        }

        if (showRightArrow) {
            mArrow.setVisibility(VISIBLE);
        } else {
            mArrow.setVisibility(GONE);
        }

        typedArray.recycle();
    }

    public void setText(String text) {
        if (StringUtils.isEmpty(text)) {
            mText.setVisibility(GONE);
        } else {
            mText.setVisibility(VISIBLE);
            mText.setText(text);
        }
    }

    public void setTitle(String title) {
        if (!StringUtils.isEmpty(title)) {
            mTitle.setText(title);
        }
    }

    public void setTitle(int title) {
        if (title != 0) {
            mTitle.setText(title);
        }
    }

    public void setTitle(int resId, String text) {
        if (resId != 0) {
            String title = getResources().getString(resId);
            mTitle.setText(String.format(title, text));
        }
    }

    public void setTitle(SpannableString ss) {
        if (ss != null) {
            mTitle.setText(ss);
        }
    }

    public void setSummary(String summary) {
        if (!StringUtils.isEmpty(summary)) {
            mSumarry.setVisibility(VISIBLE);
            mSumarry.setText(summary);
        }
    }

    public String getText() {
        return mText.getText().toString();
    }

    public void setArrowVisibility(int visibility) {
        mArrow.setVisibility(visibility);
    }

    public void showBadge(int show) {
        bvBadge.setVisibility(show);
    }
}